package com.eat.fish_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eat.common_utils.IpAndPortUtil;
import com.eat.common_utils.R;
import com.eat.fish_service.entity.Dao.DiseasesEncyclopedia;
import com.eat.fish_service.entity.Dao.FishEncyclopedia;
import com.eat.fish_service.entity.FishQuery;
import com.eat.fish_service.mapper.FishEncyclopediaMapper;
import com.eat.fish_service.service.FishEncyclopediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LWB
 * @since 2022-04-29
 */
@Service
public class FishEncyclopediaServiceImpl extends ServiceImpl<FishEncyclopediaMapper, FishEncyclopedia> implements FishEncyclopediaService {

    @Value("${uploadBaiKePath}")
    String uploadBaiKePath;

    @Autowired
    private FishEncyclopediaService fishEncyclopediaService;

    @Override
    public R addFishBaiKePic(MultipartFile pic) {
        if (pic != null) {
            String fileName = System.currentTimeMillis() + pic.getOriginalFilename();

            //第一次运行的时候，这个文件所在的目录往往是不存在的，这里需要创建一下目录
            File dir = new File(uploadBaiKePath);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            //把浏览器上传的文件复制到希望的位置
            File targetFile = new File(uploadBaiKePath, fileName);
            try {
                pic.transferTo(targetFile);
            } catch (IOException e) {
                e.printStackTrace();
                return R.error().message("上传失败");
            }

            //服务器的ip和端口号
            String ipAndPort = IpAndPortUtil.getIpAndPort();
            String picUrl = "http://"+ipAndPort+"/getFishBaiKe/"+fileName;

            //返回获取图片的url
            return R.ok().data("picUrl", picUrl);

        } else {
            return R.error().message("上传的文件为空！");
        }
    }

    @Override
    public R addFishBaiKe(String fishName, String introduction, String fishImg, String author) {

        FishEncyclopedia fishEncyclopedia = new FishEncyclopedia();

        fishEncyclopedia.setFishName(fishName);
        fishEncyclopedia.setIntroduction(introduction);
        fishEncyclopedia.setFishImg(fishImg);
        fishEncyclopedia.setAuthor(author);
        fishEncyclopedia.setIsDeleted(0);

        fishEncyclopedia.setGmtCreate(new Date());
        fishEncyclopedia.setGmtModified(new Date());



        int insert = baseMapper.insert(fishEncyclopedia);

        if (insert == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @Override
    public R pageFishBaiKeCondition(Long current, Long size, FishQuery fishQuery) {
        //创建page
        Page<FishEncyclopedia> pageFishBaiKe = new Page<>(current, size);
        //构建条件
        QueryWrapper<FishEncyclopedia> wrapper = new QueryWrapper<>();
        if(fishQuery!=null){
            //多条件查询
            String fishname = fishQuery.getFishName();
            String author = fishQuery.getAuthor();
            String TimeBegin = fishQuery.getTimeBegin();
            String TimeEnd = fishQuery.getTimeEnd();
            String introduction = fishQuery.getIntroduction();

            wrapper.eq("is_deleted",0);
            if(!StringUtils.isEmpty(fishname)) {
                wrapper.eq("fish_name",fishname);
            }
            if(!StringUtils.isEmpty(author)) {
                wrapper.eq("author",author);
            }
            if(!StringUtils.isEmpty(introduction)) {
                wrapper.like("introduction",introduction);
            }
            if(!StringUtils.isEmpty(TimeBegin)) {
                wrapper.ge("gmt_create",TimeBegin);
            }
            if(!StringUtils.isEmpty(TimeEnd)) {
                wrapper.le("gmt_create",TimeEnd);
            }
        }


        //调用方法实现查询
        fishEncyclopediaService.page(pageFishBaiKe,wrapper);

        long total = pageFishBaiKe.getTotal();//获取总记录数
        List<FishEncyclopedia> records = pageFishBaiKe.getRecords();//获取分页后的list集合
        long pages = pageFishBaiKe.getPages();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);
        if (current > pages) {
            return R.error().code(13).message("超过总页数");
        }else {
            return R.ok().data(map).data("maxPages",pages);
        }
    }


    @Override
    public R getFishBaiKe(String fishId) {

        FishEncyclopedia fishEncyclopedia = fishEncyclopediaService.getById(fishId);

        return R.ok().data("diseasesEncyclopedia",fishEncyclopedia);
    }

    @Override
    public R getAllFish() {
        List<FishEncyclopedia> list = baseMapper.selectList(null);
        ArrayList<String> fishList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            fishList.add(list.get(i).getFishName());
        }
        return R.ok().data("fishList", fishList);
    }

}
